<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Traits\ModelTraits;

use Admin;
use Illuminate\Support\Facades\DB;
use Spatie\Activitylog\Traits\LogsActivity;

trait LogsActivityTrait
{
    use LogsActivity;

    protected static $logOnlyDirty = true;

    protected static $logName = '';

    protected static $logUnguarded = true;

    protected static $ignoreChangedAttributes = ['updated_at'];

    protected function getDescriptionForEvent(string $eventName): string
    {
        $description = '';
        if (! Admin::user()) {
            $this->disableLogging();
            return '';
        }
        $adminUserName = '管理员' . Admin::user()->name;
        $name = $this->getLogName();
        $active = $name . 'id为' . $this->id . '的数据';
        switch ($eventName) {
            case 'created':
                $description = "{$adminUserName}添加了{$active}";
                break;
            case 'updated':
                $description = "{$adminUserName}修改了{$active}";
                break;
            case 'deleted':
                $description = "{$adminUserName}删除了{$active}";
                break;
        }
        return $description;
    }

    protected function getLogName(): string
    {
        $databaseName = config('database.connections.mysql.database');
        $name = DB::table('information_schema.TABLES')
            ->where('TABLE_SCHEMA', $databaseName)
            ->where('TABLE_NAME', $this->table)
            ->value('TABLE_COMMENT') ?? $this->table;
//        $name = $dictionaries[$this->table] ?? $this->table;
        self::$logName = $name;
        return "{$name} <{$this->table}>";
    }
}
